package cn.com.nes.site.action.monitor;

import cn.com.nes.common.util.DBUtils;
import cn.com.nes.site.entity.DataRes;
import cn.com.nes.site.entity.PageData;
import cn.com.nes.site.module.service.monitor.WarningService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.SQLException;

/**
 * 预警规则接口
 */
@Controller("warning")
@RequestMapping("/warning")
public class WarningAction {

    @Resource
    private WarningService service;

    /**
     * 获取预警查询条件数据   预警规则页面和故障页面共用接口
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(method = RequestMethod.GET, value = "/getQueryDataForWarningRules")
    @ResponseBody
    public PageData getQueryDataForWarningRules(HttpServletRequest request, HttpServletResponse response) throws Exception {
        DataRes reqStatus = DBUtils.setRemoteVisit(request, response, true);
        PageData res;
        if (reqStatus.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(reqStatus.getRetCode());
            res.setRetMessage(reqStatus.getRetMessage());
            return res;
        }
        res = service.getQueryDataForWarningRules(reqStatus.getReq());
        return res;
    }

    /**
     * 获取设备和数据项
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(method = RequestMethod.GET, value = "/getDeviceAndItemForWarningRules")
    @ResponseBody
    public PageData getDeviceAndItemForWarningRules(HttpServletRequest request, HttpServletResponse response) throws Exception {
        DataRes reqStatus = DBUtils.setRemoteVisit(request, response, true);
        PageData res;
        if (reqStatus.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(reqStatus.getRetCode());
            res.setRetMessage(reqStatus.getRetMessage());
            return res;
        }
        res = service.getDeviceAndItemForWarningRules(reqStatus.getReq());
        return res;
    }

    /**
     * 获取预警规则列表-分页查询
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(method = RequestMethod.GET, value = "/getWarningRules")
    @ResponseBody
    public PageData getWarningRules(HttpServletRequest request, HttpServletResponse response) throws Exception {
        DataRes reqStatus = DBUtils.setRemoteVisit(request, response, true);
        PageData res;
        if (reqStatus.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(reqStatus.getRetCode());
            res.setRetMessage(reqStatus.getRetMessage());
            return res;
        }
        res = service.getWarningRules(reqStatus.getReq());
        return res;
    }

    /**
     * 新增预警规则
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(method = RequestMethod.POST, value = "/addWarningRules")
    @ResponseBody
    public PageData addWarningRules(HttpServletRequest request, HttpServletResponse response) throws Exception {
        DataRes reqStatus = DBUtils.setRemoteVisit(request, response, true);
        PageData res;
        if (reqStatus.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(reqStatus.getRetCode());
            res.setRetMessage(reqStatus.getRetMessage());
            return res;
        }
        res = service.addWarningRules(reqStatus.getReq());
        return res;
    }

    /**
     * 删除预警规则   可批量
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(method = RequestMethod.POST, value = "/deleteWarningRules")
    @ResponseBody
    public PageData deleteWarningRules(HttpServletRequest request, HttpServletResponse response) throws Exception {
        DataRes reqStatus = DBUtils.setRemoteVisit(request, response, true);
        PageData res;
        if (reqStatus.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(reqStatus.getRetCode());
            res.setRetMessage(reqStatus.getRetMessage());
            return res;
        }
        res = service.deleteWarningRules(reqStatus.getReq());
        return res;
    }

    /**
     * 当前预警列表
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(method = RequestMethod.GET, value = "/getNewWarningList")
    @ResponseBody
    public PageData getNewWarningList(HttpServletRequest request, HttpServletResponse response) {
        DataRes reqStatus = DBUtils.setRemoteVisit(request, response, true);
        PageData res;
        if (reqStatus.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(reqStatus.getRetCode());
            res.setRetMessage(reqStatus.getRetMessage());
            return res;
        }
        res = service.getNewWarningList(reqStatus.getReq());
        return res;
    }
    /**
     * 查询预警发送列表
     * @param request
     * @param response
     * @param modelMap
     * @return
     */
    @RequestMapping(method = RequestMethod.POST, value = "/getWarningInfoList")
    public @ResponseBody
    PageData getWarningInfoList(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap) {
        DataRes reqStatus = DBUtils.setRemoteVisit(request, response, true);
        PageData res;
        if (reqStatus.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(reqStatus.getRetCode());
            res.setRetMessage(reqStatus.getRetMessage());
            return res;
        }
        res = service.getWarningInfoList(reqStatus.getReq());
        return res;
    }

    /**
     * 新增预警发送规则
     * @param request
     * @param response
     * @param modelMap
     * @return
     */
    @RequestMapping(method = RequestMethod.POST, value = "/addWarningInfo")
    public @ResponseBody
    PageData addWarningInfo(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap) throws SQLException {
        DataRes reqStatus = DBUtils.setRemoteVisit(request, response, true);
        PageData res;
        if (reqStatus.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(reqStatus.getRetCode());
            res.setRetMessage(reqStatus.getRetMessage());
            return res;
        }
        res = service.addWarningInfo(reqStatus.getReq());
        return res;
    }

    /**
     * 删除预警发送规则
     * @param request
     * @param response
     * @param modelMap
     * @return
     */
    @RequestMapping(method = RequestMethod.POST, value = "/delWarningInfo")
    public @ResponseBody
    PageData delWarningInfo(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap) throws SQLException {
        DataRes reqStatus = DBUtils.setRemoteVisit(request, response, true);
        PageData res;
        if (reqStatus.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(reqStatus.getRetCode());
            res.setRetMessage(reqStatus.getRetMessage());
            return res;
        }
        res = service.delWarningInfo(reqStatus.getReq());
        return res;
    }


}
